#include "header.h"

/* -------------------------------------------------------------------------- */
/* Reads the point and connectivity information from a file */
void read_grid(struct node** pp){
/* -------------------------------------------------------------------------- */
int i,j,kkk, neigh, idummy, FIRSTNODENUM=0;
double dx, dy, dz, t1[3], t2[3], n[3];
struct node* p;
FILE *fptr, *fvis;

fprintf(stderr, "Reading grid data ...... \n"); fflush(NULL);
fptr = fopen(GridFile,"r");
if(fptr == NULL){
	fprintf(stderr, "Grid file %s is missing.\n", GridFile);
	exit(0);
	}
//fread(&no_grid_points,sizeof(int),1,fptr);
fscanf(fptr, "%d", &no_grid_points);
kkk = 0.05*no_grid_points;
printf("Number of grid points = %d\n",no_grid_points); fflush(NULL);
*pp = (struct node *)calloc(no_grid_points,sizeof(struct node));
p   = *pp;
if(p == NULL){
    fprintf(stderr, "Memory allocation was not successful\n");
	fprintf(stderr, "Number of grid points = %d\n",no_grid_points);
    exit(0);
    }
fprintf(stderr, "Reading grid file  [");
for(i=0;i<no_grid_points;i++){
    /*
	//fread(&p[i].blk,sizeof(int),1,fptr);
	fread(&p[i].x,sizeof(double),1,fptr);
	fread(&p[i].y,sizeof(double),1,fptr);
	fread(&p[i].z,sizeof(double),1,fptr);
	fread(&p[i].point_type,sizeof(int),1,fptr);
	if(p[i].point_type==SOLID_WALL){
		p[i].t1= (double*)calloc(3,sizeof(double));
		p[i].t2= (double*)calloc(3,sizeof(double));
		p[i].n = (double*)calloc(3,sizeof(double));
	    fread(p[i].t1,sizeof(double),3,fptr);
	    fread(p[i].t2,sizeof(double),3,fptr);
		fread(p[i].n,sizeof(double),3,fptr);
	}else{
		fread(n,sizeof(double),3,fptr);
	    fread(t1,sizeof(double),3,fptr);
	    fread(t2,sizeof(double),3,fptr);
	}
	fread(&p[i].total_conn,sizeof(int),1,fptr);

	p[i].Conn      = (int *)calloc(p[i].total_conn,sizeof(int));
	if(p[i].Conn == NULL){
		printf("Error in memory allocation for p.Conn at node %d\n",i);
		fflush(NULL);
		exit(0);
		}
	fread(p[i].Conn,sizeof(int),p[i].total_conn,fptr);
    */


	/* Read ascii grid file */
	fscanf(fptr, "%d", &idummy);
    if(i==0) FIRSTNODENUM = idummy;
	fscanf(fptr, "%lf", &p[i].x);
	fscanf(fptr, "%lf", &p[i].y);
	fscanf(fptr, "%lf", &p[i].z);
	fscanf(fptr, "%d", &p[i].point_type);
	if(p[i].point_type==SOLID_WALL){
		p[i].t1= (double*)calloc(3,sizeof(double));
		p[i].t2= (double*)calloc(3,sizeof(double));
		p[i].n = (double*)calloc(3,sizeof(double));
	    for(j=0;j<3;j++) fscanf(fptr, "%lf", &p[i].t1[j]);
	    for(j=0;j<3;j++) fscanf(fptr, "%lf", &p[i].t2[j]);
	    for(j=0;j<3;j++) fscanf(fptr, "%lf", &p[i].n[j]);
	}else{
	    for(j=0;j<3;j++) fscanf(fptr, "%lf", &t1[j]);
	    for(j=0;j<3;j++) fscanf(fptr, "%lf", &t2[j]);
	    for(j=0;j<3;j++) fscanf(fptr, "%lf", &n[j]);
	}
	fscanf(fptr, "%d", &p[i].total_conn);

	p[i].Conn      = (int *)calloc(p[i].total_conn,sizeof(int));
	if(p[i].Conn == NULL){
		printf("Error in memory allocation for p.Conn at node %d\n",i);
		fflush(NULL);
		exit(0);
		}
	for(j=0;j<p[i].total_conn;j++) fscanf(fptr, "%d", &p[i].Conn[j]);
	for(j=0;j<p[i].total_conn;j++) p[i].Conn[j] -= FIRSTNODENUM;

	p[i].X = (double*)calloc(p[i].total_conn, sizeof(double));
	p[i].Y = (double*)calloc(p[i].total_conn, sizeof(double));
	p[i].Z = (double*)calloc(p[i].total_conn, sizeof(double));

	if( (i+1)%kkk == 0){ fprintf(stderr, "*"); fflush(NULL); }
	}
fclose(fptr);
fprintf(stderr, "]");

printf("\n Done.\n"); fflush(NULL);


}
/* -------------------------------------------------------------------------- */
